#!/bin/bash
# Test script for moparse with sv-parser integration
# Tests parsing, error tolerance, and static compilation

set -e  # Exit on error

echo "=== moparse Integration Tests ==="
echo

# Colors for output
GREEN='\033[0;32m'
RED='\033[0;31m'
YELLOW='\033[1;33m'
NC='\033[0m' # No Color

PASSED=0
FAILED=0

# Test function
test_case() {
    local name="$1"
    shift
    echo -n "Testing: $name ... "
    if "$@" > /dev/null 2>&1; then
        echo -e "${GREEN}PASS${NC}"
        ((PASSED++))
        return 0
    else
        echo -e "${RED}FAIL${NC}"
        ((FAILED++))
        return 1
    fi
}

# Build test
echo "Step 1: Building project..."
cargo build --release || { echo -e "${RED}Build failed${NC}"; exit 1; }
echo -e "${GREEN}✓ Build successful${NC}"
echo

# Unit tests
echo "Step 2: Running unit tests..."
cargo test || { echo -e "${RED}Unit tests failed${NC}"; exit 1; }
echo -e "${GREEN}✓ Unit tests passed${NC}"
echo

# Functional tests
echo "Step 3: Running functional tests..."
cd ../tests

# Clean up previous test outputs
rm -f test_*.modb

# Test 1: Basic parsing
test_case "Parse single module" \
    ../moparse-rs/target/release/mo parse -v core.v -m core -o test_core.modb

# Test 2: Module merging
test_case "Merge modules" \
    ../moparse-rs/target/release/mo parse -v core.v -m counter_8bit -e test_core.modb

# Test 3: Show module info
test_case "Show module info" \
    ../moparse-rs/target/release/mo show -i test_core.modb -m core

# Test 4: Signal tracing with _stub fallback
test_case "Signal tracing with stub fallback" \
    ../moparse-rs/target/release/mo connect -i test_core.modb -m core --from m3_core_grp_dn_u --to u_counter_8bit

# Test 5: Hierarchy display
test_case "Hierarchy display" \
    ../moparse-rs/target/release/mo hierarchy -i test_core.modb -m core -l 2

# Test 6: Parse all modules
test_case "Parse all modules" \
    ../moparse-rs/target/release/mo parse -v core.v --all -o test_all.modb

# Test 7: sv-parser fallback (should fail gracefully and suggest regex)
echo -n "Testing: sv-parser fallback mechanism ... "
if ../moparse-rs/target/release/mo parse -v core.v --use-sv-parser -o test_sv.modb 2>&1 | grep -q "Predefined macros"; then
    echo -e "${GREEN}PASS${NC} (fallback triggered correctly)"
    ((PASSED++))
else
    echo -e "${RED}FAIL${NC}"
    ((FAILED++))
fi

echo
cd ../moparse-rs

# Static compilation test
echo "Step 4: Testing static compilation..."
echo "Building for x86_64-unknown-linux-musl..."
./build_musl.sh || { echo -e "${RED}Static build failed${NC}"; exit 1; }

if [ -f target/x86_64-unknown-linux-musl/release/mo ]; then
    echo -e "${GREEN}✓ Static binary created${NC}"
    file target/x86_64-unknown-linux-musl/release/mo
    ls -lh target/x86_64-unknown-linux-musl/release/mo
    
    # Verify it's static
    if file target/x86_64-unknown-linux-musl/release/mo | grep -q "static"; then
        echo -e "${GREEN}✓ Confirmed: Binary is statically linked${NC}"
        ((PASSED++))
    else
        echo -e "${RED}✗ Warning: Binary may not be fully static${NC}"
        ((FAILED++))
    fi
else
    echo -e "${RED}✗ Static binary not found${NC}"
    ((FAILED++))
fi

echo
echo "=== Test Summary ==="
echo -e "Passed: ${GREEN}$PASSED${NC}"
echo -e "Failed: ${RED}$FAILED${NC}"
echo

if [ $FAILED -eq 0 ]; then
    echo -e "${GREEN}All tests passed!${NC}"
    exit 0
else
    echo -e "${RED}Some tests failed${NC}"
    exit 1
fi
